<?php
namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use App\Http\Middleware\VerifyCsrfToken;
use App\Models\aa;
use Illuminate\Http\Request;

class UserController extends Controller
{
    public function index()
    {
        return view('index.index');
    }

    public function affirm_order(Request $request)
    {
        if ($request->isMethod('post')) {
            //房屋面积
            $shuju = $request['shuju'];
            //开始时间
            $btext = $request['btext'];
            $time =date("Y-m-d $btext");
            //地址id
            $id = $request['ids'];
            //开始时间到结束时间加两小时
            $a=strtotime("$btext 2 hours");
            //结束时间
            $over_time =date('Y-m-d H:i',$a);
            //为site字段查找地址
            $rew=DB::table('address')->where('id',$id)->first();
            $where =[
                'matter' => "日常保洁",
                'add_id' => $id,
                'casa' => $shuju,
                'time' => $time,
                'over_time' =>$over_time,
                'uid' => session('userinfo')['id'],
                'site' =>$rew->dz
            ];
            $row=DB::table('affirm')->insert($where);
            if($row){
                return '1';
            }

        }else if($request->isMethod('get')){
            //检测是否有空闲的阿姨，如果阿姨一天有四次保洁过了，就不会再给阿姨分配工作
            $rows=DB::table('people')->where('state','1')->first();
/*            if($rows->number == 4){
                die;
            }*/
            if($rows == ""){
                echo "<script> alert('已经没有阿姨可以匹配了');parent.location.href='/'; </script>";
                die;
            }
            $res=DB::table('affirm')->where('uid',session('userinfo')['id'])->orderBy('id','Desc')->first();
            $roo=DB::table('address')->where('id',$res->add_id)->first();
            $rows=DB::table('agent')->limit('2')->get();
            return view('index.affirm_order',['res'=>$res,'roo'=>$roo,'rows'=>$rows]);
        }
    }

    public function evaluate(Request $request)
    {
        if ($request->isMethod('get')) {
            $id = request('id');
            $ros=DB::table('affirm')->where('id',$id)->first();
            $res=DB::table('people')->where('id',$ros->aunt_id)->first();
            $obj = DB::table('user')->where('id',session('userinfo')['id'])->first();
            return view('index.evaluate',['res'=>$res , 'obj'=>$obj,'ros'=>$ros]);
        }else if($request->isMethod('post')){
            $attitude = request('aunt');
            $quality = request('auny');
            $text = request('text');
            $name = request('name');
            $time=date('Y-m-d H:i:s');
            $content = request('content');
            $sid = request('sid');
            $id = request('id');
            $where=[
                'attitude' => $attitude,
                'quality' => $quality,
                'text' => $text,
                'name' => $name,
                'time' => $time,
                'content' => $content,
                'serviceid' => $sid
            ];
            $res=DB::table('appraise')->insert($where);
            $row=DB::table('affirm')->where('id',$id)->update(['state'=>3]);
            if($res){
                return '1';
            }
        }
    }

    public function floor(Request $request)
    {
        if ($request->isMethod('get')) {
            $text=$request['text'];
            $id=$request['id'];
            //时间
            $start = strtotime(date("Y-m-d 8:00"));
            $end = strtotime(date("Y-m-d 15:30"));
            for ($time = $start; $time <= $end; $time = $time + 30 * 60) {
                $res[] = date("H:i", $time);
            }
            return view('index.floor',['text'=>$text,'res'=>$res,'id'=>$id]);
        }
    }

    public function matching(Request $request)
    {
        if ($request->isMethod('post')) {
            $pid = request('pid');
            $id = request('id');
            $money = request('money');
            //订单号
            date_default_timezone_set("PRC");
            $time = strtotime(date("Y-m-d H:i:s",time()));
            $order = $time . $id;
            //订单生成时间
            $create_time=date('Y-m-d H:i:s');
            $row=DB::table('affirm')->where('id',$id)->update(['money'=>$money,'order'=>$order,'pid'=>$pid,'create_time'=>$create_time]);
            if($row){
                return "1";
            }
        }else if($request->isMethod('get')){
            //回显订单
            $res=DB::table('affirm')->where('uid',session('userinfo')['id'])->orderBy('id','Desc')->first();
            if($res->pid == 3){
                $resT = DB::table('agent')->get();
            }else{
                $resT = DB::table('agent')->where('id',$res->pid)->limit(1)->get();
            }
            //查出选中的地址
            $roo=DB::table('address')->where('id',$res->add_id)->first();
            //查出空闲的阿姨分配
            $rows=DB::table('people')->where('state','1')->first();
            //修改阿姨状态
            $aunt=DB::table('people'    )->where('id',$rows->id)->update(['state'=>0,'content'=>$res->matter]);
            //修改订单
            $book=DB::table('affirm')->where('id',$res->id)->update(['aunt_id'=>$rows->id]);
            return view('index.matching',['res'=>$res,'roo'=>$roo,'resT' => $resT,'rows'=>$rows]);
        }
    }

    public function payment_order(Request $request)
    {
        if($request->isMethod('get')){
            $id=$request['id'];
            $res=DB::table('affirm')->where('id',$id)->first();
            if($res->pid == 3){
                $resT = DB::table('agent')->get();
            }else{
                $resT = DB::table('agent')->where('id',$res->pid)->limit(1)->get();
            }
            //查出选中的地址
            $roo=DB::table('address')->where('id',$res->add_id)->first();
            //查出阿姨分配
            $rows=DB::table('people')->where('id',$res->aunt_id)->first();
            //修改阿姨状态
            $aunt=DB::table('people')->where('id',$rows->id)->update(['state'=>0,'content'=>$res->matter]);
            //修改订单
            $book=DB::table('affirm')->where('id',$res->id)->update(['aunt_id'=>$rows->id]);
            return view('index.payment_order',['res'=>$res,'roo'=>$roo,'rows'=>$rows,'resT' => $resT]);
        }
    }

    public function myorder(Request $request)
    {
//        $rows=DB::table('affirm')->where(['uid' => session('userinfo')['id'] and 'state','>=','1'])->get();
        $rows=DB::table('affirm')->where([
            ['uid' , '=' ,  session('userinfo')['id']],
            ['state','<=','1']
        ])->get();
        foreach ($rows as $v){
            if($v->state == 0){
                //往后退一小时
                $a = strtotime("$v->create_time  1 hours");
                //获取当前时间
                $tiem = strtotime(date('Y-m-d H:i:s'));
                //当前时间大于或等于我数据库中的时间，则订单失效
                if($tiem >= $a){
                    $res=DB::table('affirm')->where('id',$v->id)->delete();
                }
            }
        }
        $res=DB::table('affirm')->where([
            ['uid','=', session('userinfo')['id']],
            ['state','>', 1]
        ])->get();
        if(count($rows)  == 0)$rows=null;
        return view('index.myorder',['rows'=>$rows,'res' => $res]);
    }

    public function myorder_none()
    {
        return view('index.myorder_none');
    }

    public function news()
    {
        return view('index.news');
    }

    public function problem_mould()
    {
        return view('index.problem_mould');
    }

    public function order(Request $request)
    {
        $id=$request['id'];
        $row= DB::table('affirm')->where('id',$id)->first();
        //查出阿姨信息
        $res=DB::table('people')->where('id',$row->aunt_id)->first();
        //获取当前时间
        date_default_timezone_set("PRC");
        $time = date("Y-m-d H:i:s");
        if($time >= $row->over_time){
            $roo=DB::table('affirm')->where('id',$id)->update(['state' => 2]);
            $rew=DB::table('people')->where('id',$res->id)->update(['state' => 1,'orders'=>$res->orders+1]);
        }
        return view('index.order',['res'=>$res,'row'=>$row]);
    }

    public function affirm_start(Request $request)
    {
        if ($request->isMethod('post')) {
            //面积
            $number = $request['number'];
            //开始时间
            $btext = $request['btext'];
            $times =date("Y-m-d $btext");
            //地址id
            $id = $request['ids'];
            //服务内容
            $text = $request['text'];
            //总价
            $money = $request['cost'];
            //订单号
            date_default_timezone_set("PRC");
            $time = strtotime(date("Y-m-d H:i:s",time()));
            $order = $time . $id;
            //订单生成时间
            $create_time=date('Y-m-d H:i:s');
            //开始时间到结束时间加两小时
            $a=strtotime("$btext 2 hours");
            //结束时间
            $over_time =date('Y-m-d H:i',$a);
            //为site字段查找地址
            $rew=DB::table('address')->where('id',$id)->first();
            //查找有空闲的阿姨
            $rows=DB::table('people')->where('state','1')->orderBy('id','Desc')->first();
            if($rows == ""){
                echo "<script> alert('已经没有阿姨可以匹配了');parent.location.href='/'; </script>";
                die;
            }
            $where =    [
                'matter' => $text,
                'add_id' => $id,
                'casa' => $number,
                'time' => $times,
                'over_time' =>$over_time,
                'uid' => session('userinfo')['id'],
                'order' => $order,
                'money' => $money,
                'site' => $rew->dz,
                'aunt_id' =>$rows->id,
                'create_time' => $create_time
            ];
            $row=DB::table('affirm')->insert($where);
            if($row){
                return '1';
            }
        }else if($request->isMethod('get')){
            $rows=DB::table('people')->where('state','1')->orderBy('id','Desc')->first();
            $res=DB::table('affirm')->where('uid',session('userinfo')['id'])->orderBy('id','Desc')->first();
            $roo=DB::table('address')->where('id',$res->add_id)->first();
            return view('index.affirm_start',['res'=>$res,'roo'=>$roo,'rows'=>$rows]);
        }
    }

    public function order_underway()
    {
        return view('index.order_underway');
    }

    public function cleaning_index(Request $request)
    {
        if ($request->isMethod('get')) {
            $id = $request['id'];
            $start = strtotime(date("Y-m-d 8:00"));
            $end = strtotime(date("Y-m-d 15:30"));
            for ($time = $start; $time <= $end; $time = $time + 30 * 60) {
                $res[] = date("H:i", $time);
            }
            $weekarray=array("日","一","二","三","四","五","六");
            for($i = 0 ; $i < count($weekarray) ; $i++){
                $roo[]= "星期".$weekarray[date("w",strtotime(date("Y-m-d" , strtotime("+$i day"))))];
            }
            //查找有空闲的阿姨
            $rows=DB::table('people')->where('state','1')->orderBy('id','Desc')->first();
//            if($rows == ""){
//                echo "<script> alert('今天的阿姨已经全都预约好了');parent.location.href='/'; </script>";
//                die;
//            }
            return view('index.cleaning_index', ['res' => $res, 'id' => $id , 'roo' => $roo, 'rows' =>$rows]);
        }else if ($request->isMethod('post')) {
            $id = $request['id'];
            $text =$request['text'];
            $json = ['code' => '1', 'id' => $id];
            $jsos = ['code' => '2', 'id' => $id];
            if($text != ""){
                return $jsos;
            }
            if ($id != "") {
                return $json;
            }
        }
    }

    public function share()
    {
        return view('index.share');
    }
    public function success_indent()
    {
        return view('index.success_indent');
    }
    public function payment(Request $request)
    {
        if($request->isMethod('get')){
            $id=$request['id'];
            $res = DB::table('affirm')->where('id',$id)->first();
            //查找用户拥有并且满足条件的优惠券
            $rows = DB::table('coupons')->where([
                ['lid','=', session('userinfo')['id']],
                ['shold', '<', $res->money],
                ['bjstate', '=' , 1]
            ])->get();
            //如果get查询不到返回的是一个结果集，需要在查询字段后面加上toArray()或用count判断
            if(count($rows) == 0){
                $rows = null;
            }
            //查询用户还要多少余额
            $row=DB::table('user')->where('id', session('userinfo')['id'])->first();
            $balance=$row->balance;
            return view('index.payment',['rows'=>$rows,'balance'=>$balance,'res'=>$res]);
        }else if($request->isMethod('post')){
            $mon=$request['mon'];
            $money=$request['money'];
            $id=$request['id'];
            $aid=$request['aid'];
            $balance=$request['balance'];
            $monn=$money-$mon;
            if($balance < $monn){
                return '-1';
                exit();
            }
            $time=date('Y-m-d H:i:s');
            $balances=$balance-$monn;
            $roo=DB::table('user')->where('id',session('userinfo')['id'])->update(['balance'=>$balances]);
            $row=DB::table('affirm')->where('id',$id)->update(['money'=>$monn,'state'=>1,'payment_time'=>$time]);
            $res=DB::table('coupons')->where('id',$aid)->update(['bjstate'=>0]);
            if($row){
                return '1';
            }
        }
    }

    public function WeiMeng(Request $request)
    {
        $id = $request['id'];
        $res=DB::table('agent')->where('id',$id)->first();
        return view('index.WeiMeng',['res'=>$res]);
    }

    public function waiter_details(Request $request)
    {
        if ($request->isMethod('get')) {
            $id = $request['id'];
            $row = DB::table('people')->where('id', $id)->first();
            $res = DB::table('appraise')->where('serviceid', $row->id)->limit(5)->get();
            return view('index.waiter_details', ['row' => $row, 'res' => $res]);
        }
    }

    public function protocol()
    {
        return view('index.protocol');
    }
    //设置
    public function my_zh()
    {
        return view('index.my_zh');
    }

    public function index_content(Request $request)
    {
        if ($request->session()->has('userinfo')) {

            $id = session('userinfo')['id']; //获取登录时的用户id

            $res = DB::table('user')->where('id',session('userinfo')['id'])->first();//查询登录时用户的id回显余额;

            //判断余额是否等于或大于1000，大于等于1000即可成为我店会员
            if($res->balance>=1000){

                DB::table('user')->where('id',$id)->update(['member'=>'1']);

            }else if($res->balance<1000){

                DB::table('user')->where('id',$id)->update(['member'=>'0']);

            }

            if(session('userinfo')['id']==""){

                return view('index.index_content');

            }

            $aa = session('userinfo')['id'];

            $row = DB::table('user')->where('id',$aa)->first();

            return view('index.index_content',['row'=>$row]);
        }

            return view('index.index_content');

    }

    public function my_money(Request $request)
    {
        if ($request->session()->has('userinfo')) {

          $aa = session('userinfo')['id'];

          $row = DB::table('user')->where('id',$aa)->first();

          return view('index.my_money',['row'=>$row]);

        }

           echo "<script>alert('请先登录,再来查看噢'); location.href='index_content'</script>";

    }
    //我的设置账号显示：
    public function dind_out()
    {

        $aa = session('userinfo')['id'];

        $row = DB::table('user')->where('id',$aa)->first();

        return view('index.dind_out',['row'=>$row]);

    }
    //我的设置账号功能：
    public function dind_yes(Request $request)
    {
        $rows = $request->input();

        $aa = session('userinfo')['name'];

        $res = DB::table('user')->where('name',$aa)->update(['account'=>$rows['aa']]);

        if($res){

            return ['code'=>'1','name'=>'添加成功','url'=>'/index_content'];//全都验证通过后走HTML里的ajax直接跳转到登录页面

        }

            return ['code'=>'-1','name'=>'添加失败'];//没有通过验证则会提醒’注册失败‘

    }
    //修改昵称页面：
    public function add_name()
    {

        $aa = session('userinfo')['id'];

        $row = DB::table('user')->where('id',$aa)->first();

        return view('index.add_name',['row'=>$row]);

    }

    //修改昵称页面功能：
    public function name_update(Request $request)
    {

        $rows = $request->input();

        $aa = session('userinfo')['id'];

        $res = DB::table('user')->where('id',$aa)->update(['name'=>$rows['name']]);

        if($res){

            return ['code'=>'1','name'=>'修改成功','url'=>'/index_content'];

        }

            return ['code'=>'-1','name'=>'修改失败'];

    }

    //修改密码页面：
    public function add_pwd()
    {
        $aa = session('userinfo')['id'];

        $row = DB::table('user')->where('id',$aa)->first();

        return view('index.add_pwd',['row'=>$row]);

    }

    //修改密码页面功能：
    public function pwd_update(Request $request)
    {
        $rows = $request->input();

        $aa = session('userinfo')['id'];

        $res = DB::table('user')->where('id',$aa)->update(['pwd'=>md5($rows['pwd'])]);

        if($res){

            return ['code'=>'1','name'=>'修改成功','url'=>'/index_content'];

        }

            return ['code'=>'-1','name'=>'新密码不能和旧密码一致'];

    }
}
